URL = "https://osgswig.googlecode.com/hg"

# currently version indicates the osg version
VERSIONS = ["3.0.1"]
DEFAULT_VERSION = "3.0.1"
DEPENDS = ['osg', 'swig']

from source.builders import CMakeBuilder, FetchSourceHG
import os

class FetchSourceGoogleCode(FetchSourceHG):

  def fetch(self, url, downloadlocation, buildlocation ):
    targetdir = "osgswig-%s" % "3.0.1" #todo allow custom version
    targetpath = buildlocation + os.sep  + targetdir

    if os.path.exists( targetpath ):
      # hg update
      os.chdir( targetpath )
      os.system( " ".join([self.executable, "pull", url]) )
      os.system( " ".join([self.executable, "up"]) )
    else:
      # hg clone
      os.system( " ".join([self.executable, "clone", url, targetpath]) )

    return targetpath

class OsgSwigBuilder( CMakeBuilder ):
  """ subclass of CMakeBuilder with custom configure step """

  def targetpath(self):
    return self.directories.enviro + os.sep + "osgswig-%s" % self.dependencies['osg'].target.build_version
    #todo allow FetchSource to be specified like URL, or determine implicit from url

  def downloadextract(self):
    return FetchSourceGoogleCode()
    
  def configure( self ):    
    OSG_DIR = self.dependencies['osg'].targetpath()
    os.environ['OSGDIR'] = OSG_DIR
    #todo support linux
    SWIG_EXECUTABLE = " -DSWIG_EXECUTABLE=" + self.dependencies['swig'].targetpath() + os.sep + "swig.exe" + " -DSWIG_DIR=" + self.dependencies['swig'].targetpath()
    CMakeBuilder.configure(self, SWIG_EXECUTABLE)

  def hasdebug(self):
      return False    
    
  def make_devenv(self):
    os.chdir( self.extractedpath )
    self.debug = False
    self.call_devenv( "osgswig.sln", "ALL_BUILD.vcproj" )


BUILDER = OsgSwigBuilder
